package com.webvihar.access2db.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.webvihar.access2db.R;
import com.webvihar.access2db.builders.AbstractQueryBuilder;
import com.webvihar.access2db.builders.MySQLQueryBuilder;
import com.webvihar.access2db.utils.PreferencesManager;

public abstract class BaseActivity extends Activity {
	
	protected String PREF_FIRST_TIME = "first_time";
	protected String PREF_SESSION_NAME = "session_name";
	protected String PREF_SESSION_HOST = "session_host";
	protected String PREF_SESSION_PORT = "session_port"; 
	protected String PREF_SESSION_USERNAME = "session_username";
	protected String PREF_SESSION_PASSWORD = "session_password";
	protected String PREF_SESSION_DBNAME = "session_dbname";
	protected String PREF_SESSION_TABLE_NAME = "session_table_name";
	protected String PREF_SESSION_QUERY = "session_query";

	protected String DEFAULT_DATABASE_NAME = "mysql";

	// Data transfer constants (keys) for Activity Navigation
	public static final String KEY_EMAIL_ID = "emailId";
	public static final String KEY_SECURITY_CODE = "security_code";
	public static final String KEY_DATABASE_NAME = "dbname";
	public static final String KEY_TABLE_NAME = "tablename";
	public static final String KEY_QUERY = "query";
	public static final String KEY_TABLE_DATA_NAVIGATION_MODE = "table_data_navigation_mode";
	public static final String KEY_TABLE_MANAGEMENT_NAVIGATION_MODE = "table_management_navigation_mode";
	public static final String KEY_TABLE_FIELD_INDEX = "table_field_index";
	
	public static Object tableHeaders;
	public static Object tableRow;
	
	/*
	 * Display the toast message
	 */
	protected void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}
	
	protected void showLongToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}
	
	/*
	 * Get data from the bundle
	 */
	protected String getValue(String key, Bundle bundle) {
		return bundle.getString(key);
	}
	
	protected ProgressDialog getProgressDialog() {
		ProgressDialog progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(getText(R.string.dialog_waiting_message));
		return progressDialog;
	}
	
	protected AbstractQueryBuilder getQueryBuilder() {
		return (new MySQLQueryBuilder());
	}
	
	public String getDefualtDatabase() {
		return DEFAULT_DATABASE_NAME;
	}

	public void setDefualtDatabaseInPreferences(Context context) {
		PreferencesManager.setString(context, PREF_SESSION_DBNAME, DEFAULT_DATABASE_NAME);
	}
	
	public void hideView(View view, boolean hide) {
		if(hide) {
			view.setVisibility(View.GONE);
		} else {
			view.setVisibility(View.VISIBLE);
		}
	}
	
	protected TextView getTableHeaderTextView(Context context, String text) {
		TextView tv = new TextView(context);
		tv.setText(text);
		tv.setTextColor(Color.WHITE);
		tv.setBackgroundColor(Color.BLACK);
		tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_header_bg));
		tv.setPadding(5, 1, 5, 1);
		tv.setGravity(Gravity.CENTER_VERTICAL);
		tv.layout(2, 2, 2, 2);
		return tv;
	}
	
	protected TextView getTableDataTextView(Context context, String text) {
		TextView tv = new TextView(this);
		tv.setText(text); 
		tv.setPadding(2, 2, 2, 2);
		tv.setTextColor(Color.BLACK);
		tv.setBackgroundColor(Color.WHITE);
		tv.setGravity(Gravity.CENTER_VERTICAL);
		tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_data_unpressed));
		return tv;
	}
}
